package com.tarena.lbs.attach.dao.repository.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tarena.lbs.attach.dao.mapper.AttachMapper;
import com.tarena.lbs.attach.dao.repository.AttachRepository;
import com.tarena.lbs.attach.enums.FileStatusEnum;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.File;
import java.util.List;


@Slf4j
@Repository
public class AttachRepositoryImpl implements AttachRepository {

    @Autowired
    private AttachMapper attachMapper;

    @Override
    public void saveFile(AttachPO attachPO) {
        attachMapper.insert(attachPO);
    }

    @Override
    public List<AttachPO> queryAttachInfo(AttachQuery attachQuery) {
        QueryWrapper<AttachPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_type", attachQuery.getBusinessType())
                .eq("business_id", attachQuery.getBusinessId());
        return attachMapper.selectList(queryWrapper);
    }

    @Override
    public boolean batchUpdateBusiness(List<PicUpdateParam> picParams) {
        for (PicUpdateParam param : picParams) {
            if (param.getId() == null || param.getBusinessType() == null || param.getBusinessId() == null) {
                log.warn("跳过无效的更新参数: {}", param);
                continue;
            }
            AttachPO attachPO = new AttachPO();
            attachPO.setId(param.getId());
            attachPO.setBusinessType(param.getBusinessType());
            attachPO.setBusinessId(param.getBusinessId());
            attachPO.setModifiedUserId(1L);//要动态
            attachPO.setModifiedUserName("zhangsan");//要动态
            attachPO.setGmtModified(System.currentTimeMillis());
            attachMapper.updateById(attachPO);
        }
        return true;
    }

    @Override
    public boolean deleteAttachInfo(String fileUuid) {
        if (fileUuid == null || fileUuid.trim().isEmpty()) {
            log.warn("删除附件参数为空");
            return false;
        }
            // 删除数据库记录
            attachMapper.deleteById(fileUuid);
            return true;
    }

    @Override
    public AttachPO getByFileUuid(String fileUuid) {
        return attachMapper.selectById(fileUuid);
    }

    @Override
    public void updateById(AttachPO attachPO) {
        attachMapper.updateById(attachPO);
    }
}
